package com.xz.controller;

import com.xz.util.RealUtil;
import com.xz.util.UUIDGenerator;
import com.xz.util.api.vo.Result;
import net.sf.json.JSONObject;
import nuonuo.open.sdk.NNOpenSDK;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("text")
@CrossOrigin
public class Text {

    @PostMapping("/rrr")
    public Result rrr(String FileId){
        NNOpenSDK sdk = NNOpenSDK.getIntance();
        String taxnum = "2354545789"; // 授权企业税号String appKey = "Hn***XL";
        String appSecret = "9FC8809607AC483B";
        String method = "nuonuo.billRecognition.recognizeBillOpenSingle"; // API方法名 单
        String token = "370743d065d1e97e3c12430ksyt5djks"; // 访问令牌
        String content =  "{" + "\"callBackUrl\":\"http://xxxx/testCallBackSuccess.do\"," + "\"source\":\"0\"," + "\"fileId\":\""+FileId+"\"" + "}";
        String url = "https://sdk.nuonuo.com/open/v1/services"; // SDK请求地址
        String senid = UUIDGenerator.generate().replace("-", ""); // 唯一标识，由企业自己生成32位随机码
        String result = sdk.sendPostSyncRequest(url, senid, "32033532", appSecret, token, taxnum, method, content);
        JSONObject jsonObject = JSONObject.fromObject(result);
        JSONObject taskId = (JSONObject) jsonObject.get("result");
        System.out.println(taskId);
        return Result.ok(taskId);
    }


    @PostMapping("/photo")
    public Result vvv(String taskId){
        NNOpenSDK sdk = NNOpenSDK.getIntance();
        String taxnum = "2354545789"; // 授权企业税号String appKey = "Hn***XL";
        String appSecret = "9FC8809607AC483B";
        String method = "nuonuo.billRecognition.queryBillOpenByTaskId";
        String token = "370743d065d1e97e3c12430ksyt5djks"; // 访问令牌
//            String content = "{\"taskId\": \""+taskId+"\" }";
        String content = taskId;
        String url = "https://sdk.nuonuo.com/open/v1/services"; // SDK请求地址
        String senid = UUIDGenerator.generate().replace("-", ""); // 唯一标识，由企业自己生成32位随机码
        String result = sdk.sendPostSyncRequest(url, senid, "32033532", appSecret, token, taxnum, method, content);
        JSONObject jsonObject = JSONObject.fromObject(result);
        JSONObject json = JSONObject.fromObject(jsonObject.get("result")) ;
        System.out.println(json);
        String type = (String) json.get("type");
        System.out.println(type);
        if(type.equals("vat_electronic_invoice")){//增值税电子发票
            JSONObject data = JSONObject.fromObject(json.get("data"));

        }else if(type.equals("vat_roll_invoice")){//增值税卷式发票
            JSONObject data = JSONObject.fromObject(json.get("data"));
        }else if(type.equals("train_ticket")){//火车票
            JSONObject data = JSONObject.fromObject(json.get("data"));
        }else if(type.equals("vehicle_toll_invoice")){//车辆通行费
            JSONObject data = JSONObject.fromObject(json.get("data"));
        }else  if(type.equals("quota_invoice")){//定额发票
            JSONObject data = JSONObject.fromObject(json.get("data"));
        }else if(type.equals("vat_special_invoice")){//增值税专用发票
            JSONObject data = JSONObject.fromObject(json.get("data"));
            System.out.println(data);
        }else if(type.equals("vat_common_invoice")){//增值税普通发票
            JSONObject data = JSONObject.fromObject(json.get("data"));
        }else if(type.equals("taxi_ticket")){//出租车发票
            JSONObject data = JSONObject.fromObject(json.get("data"));
        }else if(type.equals("other")){//其他类型
            JSONObject data = JSONObject.fromObject(json.get("data"));
        }
        return Result.ok(json);
    }

    /**
     * 增值税电子发票
     * @param data
     * @return
     */
    public String vatElectronicInvoice(JSONObject data){
        String correctCode = (String) data.get("vat_invoice_correct_code");//校验码
        String daima = (String) data.get("vat_invoice_daima");//发票代码
        String date = (String) data.get("vat_invoice_data");//开票日期
        String flag = (String) data.get("vat_invoice_flag");//票据类型
        String goods = (String) data.get("vat_invoice_goods");//货物或应税劳务 服务 名称
        String haoma = (String) data.get("vat_invoice_haoma");//发票号码
        String payerId = (String) data.get("vat_invoice_payer_id");//购买方纳税人识别号
        String payerName = (String) data.get("vat_invoice_payer_name");//购买方公司名称
        String price = (String) data.get("vat_invoice_price");//单价金额
        String priceTotal = (String) data.get("vat_invoice_price_total");//合计金额
        String priceTaxTotalFigure = (String) data.get("vat_invoice_price_tax_total_figure");//纳税合计小写
        String quantity = (String) data.get("vat_invoice_quantity");//数量
        String sellerAddrTell = (String) data.get("vat_invoice_seller_addr_tell");//销售方地址电话
        String type = (String) data.get("vat_invoice_seller_bank_account");//销售方开户行及账号
        String sellerId = (String) data.get("vat_invoice_seller_id");//销售方纳税人识别号
        String sellerName = (String) data.get("vat_invoice_seller_name");//销售方公司名称
        String sort = (String) data.get("vat_invoice_sort");//发票类型名称
        String tax = (String) data.get("vat_invoice_tax");//税额
        String taxRate = (String) data.get("vat_invoice_tax_rate");//税率
        String taxTotal = (String) data.get("vat_invoice_tax_total");//税额合计
        String unitPrice = (String) data.get("vat_invoice_unit_price");//单价

        return "";
    }

    /**
     * 火车票字段的提取
     * @param jsonObject
     */
    public String trainTicket(JSONObject jsonObject){
        String boarding_gate = (String) jsonObject.get("boarding_gate");         //检票口
        String departure_time = (String) jsonObject.get("departure_time");       //出发时间
        String departure_station = (String) jsonObject.get("departure_station"); //始发地
        String passenger_id = (String) jsonObject.get("passenger_id");           //身份证号
        String passenger_name = (String) jsonObject.get("passenger_name");       //姓名
        String price = (String) jsonObject.get("price");                         //票价
        String seat_number = (String) jsonObject.get("seat_number");             //车厢号座位号
        String seat_type = (String) jsonObject.get("seat_type");                 //座位类别
        String terminus = (String) jsonObject.get("terminus");                   //目的地
        String ticket_id = (String) jsonObject.get("ticket_id");                 //
        String ticket_number = (String) jsonObject.get("ticket_number");         //火车票编号
        String train_number = (String) jsonObject.get("train_number");           //列车号

        return "";
    }


    /**
     * 车辆通行费发票字段的提取
     * @param jsonObject
     */
    public String vehicle(JSONObject jsonObject){
        String price = (String) jsonObject.get("price");           //收费金额
        String toll_daima = (String) jsonObject.get("toll_daima"); //发票代码
        String toll_haoma = (String) jsonObject.get("toll_haoma"); //发票号码

        return "";
    }


    /**
     * 定额发票字段的提取
     * @param jsonObject
     */
    public String quota(JSONObject jsonObject){
        String price_figure = (String) jsonObject.get("price_figure"); //汉字金额
        String price_word = (String) jsonObject.get("price_word");     //数字金额
        String quota_daima = (String) jsonObject.get("quota_daima");   //发票代码
        String quota_haoma = (String) jsonObject.get("quota_haoma");   //发票号码

        return "";

    }


    /**
     * 增值税专用发票字段的提取
     * @param jsonObject
     */
    public String special(JSONObject jsonObject){

        return "";

    }


    /**
     *增值税卷式发票
     * @param data
     * @return
     */
    public String vatRollInvoice(JSONObject data){
        String check = (String) data.get("check");//校验码
        String daima = (String) data.get("invoice_daima");//发票代码
        String date = (String) data.get("date");//开票日期
        String sellerId = (String) data.get("seller_id");//销售方纳税人识别号
        String sellerName = (String) data.get("seller_name");//销售方公司名称
        String haoma = (String) data.get("invoice_haoma");//发票号码
        String payerId = (String) data.get("payer_id");//购买方纳税人识别号
        String payerName = (String) data.get("payer_name");//购买方公司名称
        String price = (String) data.get("price_total");//单价金额
        return "";
    }
}
